<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="/jqueryeasyui/jquery.easyui.jsp"%>
<script type="text/javascript">
	var datamode;
	$(function() {
		$("#addform").window({
			title : "部门信息",
			closed : true,
			onClose:function(){
				$("#addform").form("disableValidation");
			}
		});

		$("#grid").datagrid({
			title : "部门信息！",
			iconCls : "icon-aibo5",
			nowrap : true,
			autoRowHeight : false,
			collapsible : false,
			url : "${pageContext.request.contextPath}/Dept_queryAll.action",
			idField : "deptId",
			columns : [ [ {
				field : "deptId",
				title : "部门编号"
			}, {
				field : "deptName",
				title : "部门名称",
				align : "center",
			} ] ],
			singleSelect : true,
			toolbar : [ {
				"id" : "btnAdd",
				"text" : "添加部门",
				iconCls : "icon-add",
				handler : function() {
					datamode="add";
					$("#deptName").val("");
					$("#deptId").val("");
					$("#addform").window("open");
				}
			}, {
				"id" : "btnDelete",
				"text" : "删除部门信息",
				iconCls : "icon-cut",
				handler : function() {
					var selected = $("#grid").datagrid("getSelected");
					if (selected) {
						$("#deleteDialog").dialog("open");
					}
				}
			},  {
				"id" : "btnModify",
				"text" : "修改部门信息",
				iconCls : "icon-edit",
				handler :loadModify
			},{
				"id" : "btnRefresh",
				"text" : "刷新",
				iconCls : "icon-reload",
				handler : function() {
					$("#grid").datagrid("reload");
				}
			} ]
		});

		$("#btnAddData").click(function() {
			$("#addform").form("enableValidation");
			if ($("#addform").form("validate") && "add"==datamode) {
				$.post("${pageContext.request.contextPath}/Dept_save.action", {
					"deptModel.deptName" : $("#deptName").val()
				}, showMessage, "json");
			}else if ($("#addform").form("validate") && "modify"==datamode) {
				$.post("${pageContext.request.contextPath}/Dept_modify.action", {
					"deptModel.deptId" : $("#deptId").val(),
					"deptModel.deptName" : $("#deptName").val()
				}, showMessage, "json");
			}
		});

		$("#deleteDialog").dialog({
			buttons : [ {
				text : "确定",
				handler : deleteData
			}, {
				text : "取消",
				handler : function() {
					$("#deleteDialog").dialog("close");
				}
			} ],
			closed : true
		});

		function deleteData() {
			var selected = $("#grid").datagrid("getSelected");
			$.post("${pageContext.request.contextPath}/Dept_delete.action", {
				"deptModel.deptId" : selected.deptId
			}, showMessage, "json");
		}
		
		function loadModify(){
			datamode="modify";
			var selected = $("#grid").datagrid("getSelected");
			if (selected) {
				$.post("${pageContext.request.contextPath}/Dept_queryByKey.action",{
					"deptModel.deptId" : selected.deptId
				},function(data){
					$("#deptId").val(data.data.deptId);
					$("#deptName").val(data.data.deptName);
					$("#addform").window("open");
				},"json");
			}
		}
		
		function showMessage(data){
			if (data.success) {
				$("#grid").datagrid("reload");
			}
			$("#deleteDialog").dialog("close");
			$("#addform").window("close");
			$.messager.show({
				"title" : "信息",
				"msg" : data.message
			});
		}

	});
</script>
</head>
<body>
	<div id="grid"></div>
	<form id="addform" onsubmit="return false;" method="post">
		<input type="hidden" id="deptId">
		<table>
			<tr>
				<td>部门名称:</td>
				<td><input type="text" id="deptName" class="easyui-validatebox"
					data-options="required:true,novalidate:true"></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="button" value="保存"
					id="btnAddData"></td>
			</tr>
		</table>

		<div id="deleteDialog" title="删除记录">
			<p>是否删除选中的记录？</p>
		</div>
	</form>
</body>
</html>